package priv.conceit.sc.hoxton.common.web.feign;

import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import priv.conceit.sc.hoxton.common.web.exception.WebException;
import priv.conceit.sc.hoxton.common.web.exception.WebExceptionEnum;

@Component
@Slf4j
public class ErrorDecoderConfig implements ErrorDecoder {

	@Override
	public Exception decode(String methodKey, Response response) {
		switch (HttpStatus.valueOf(response.status())) {
		case UNAUTHORIZED: {
			log.error(String.valueOf(response.status()));
			log.error(response.request().toString());
			throw new WebException(WebExceptionEnum.EX_USER_AUTH_FAILURE);
		}
		case FORBIDDEN: {
			log.error(String.valueOf(response.status()));
			log.error(response.request().toString());
			throw new WebException(WebExceptionEnum.EX_USER_ACCESS_DENIED);
		}
		case INTERNAL_SERVER_ERROR: {
			log.error(String.valueOf(response.status()));
			log.error(response.request().toString());
			throw new WebException(WebExceptionEnum.EX_SYSTEM_ERROR);
		}
		default:
			log.error(String.valueOf(response.status()));
			log.error(response.request().toString());
			throw new WebException(WebExceptionEnum.EX_SYSTEM_FALLBACK);
		}

	}

}
